package com.wangtianxiang.sxp.back.web;

import com.baidu.unbiz.fluentvalidator.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wangtianxiang.sxp.back.po.SysUser;
import com.wangtianxiang.sxp.back.vo.SysUserSaveVo;
import com.wangtianxiang.sxp.back.module.service.SysUserService;
import com.wangtianxiang.sxp.back.vo.LoginVo;
import com.wangtianxiang.sxp.common.http.FeedBackBody;
import com.wangtianxiang.sxp.common.http.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wangtianxiang.sxp.common.utils.FluentValidateUtils;
import com.wangtianxiang.sxp.common.utils.MD5Utils;

import java.util.List;


/**
 * @author wangtianxiang
 * @description 用户控制类
 * @date 2021/4/17
 */
@Api(tags = "用户管理")
@RequestMapping("/sys/user")
@RestController
@Log4j
public class UserController {


    @Autowired
    private SysUserService sysUserService;

    @GetMapping(value = "list")
    @ApiOperation(value = "获取系统用户列表")
    public FeedBackBody<List<SysUser>> list(){
        List<SysUser> list;
        try {
            log.info("测试");
            QueryWrapper query = new QueryWrapper();
            query.eq("is_delete",0);
            list = sysUserService.list(query);

            return ResponseEntity.setForSuccess(list);
        }catch (Exception e){
            log.error("UserController.list error:{}",e);
            return ResponseEntity.setForServiceError();
        }

    }


    @PostMapping(value = "delete")
    @ApiOperation(value = "删除用户")
    public FeedBackBody<?> delete(@RequestParam(value = "id") String id){

        try {
            SysUser dbUser = sysUserService.getById(id);
            if(dbUser == null){
                return ResponseEntity.setForDataError("删除的用户不存在");
            }
            dbUser.setIsDelete(1);
            sysUserService.updateById(dbUser);
            return ResponseEntity.setForSuccess();
        }catch (Exception e){
            log.error("UserController.list delete:{}",e);
            return ResponseEntity.setForServiceError();
        }

    }


    @PostMapping(value = "saveUser")
    @ApiOperation(value = "新增系统用户")
    public FeedBackBody<?> saveUser(@RequestBody SysUserSaveVo req){

        try {
            //1、参数校验
            Result result = FluentValidateUtils.toSimpleValidate(req);
            if (!result.isSuccess()){
                return ResponseEntity.setForParamCheckError().setDetail(result.getErrors().get(0));
            }

            //todo 对密码进行正则校验
            QueryWrapper<SysUser> query = new QueryWrapper<>();
            query.eq("username",req.getUsername());
            query.eq("is_delete",0);
            SysUser dbUser = sysUserService.getOne(query);

            if(dbUser != null){
                return ResponseEntity.setForDataError("用户已存在");
            }

            SysUser sysUser = new SysUser();
            BeanUtils.copyProperties(req,sysUser);
            sysUserService.saveUser(sysUser);

            return ResponseEntity.setForSuccess();
        }catch (Exception e){
            log.error("UserController.saveUser error:{}",e);
            return ResponseEntity.setForServiceError();
        }

    }


    @PostMapping(value = "login")
    @ApiOperation(value = "登录")
    public FeedBackBody<?> login(@RequestBody LoginVo req){
        try {
            //1、参数校验
            Result result = FluentValidateUtils.toSimpleValidate(req);
            if (!result.isSuccess()){
                return ResponseEntity.setForParamCheckError().setDetail(result.getErrors().get(0));
            }

            //todo 前端传输密码的时候用算法加密下 不要明文传输,后台解密后再用md5加密与数据库的密码比较是否一致
            //todo 加上验证码字段校验和验证码接口
            QueryWrapper<SysUser> query = new QueryWrapper<>();
            query.eq("username",req.getUsername());
            query.eq("is_delete",0);
            SysUser dbUser = sysUserService.getOne(query);
            if(dbUser == null){
                return ResponseEntity.setForDataError("用户名不存在");
            }

            if(!MD5Utils.getSaltverifyMd5AndSha(req.getPassword(),dbUser.getPassword())){
                return ResponseEntity.setForDataError("密码不正确");
            }

            return ResponseEntity.setForSuccess(dbUser);
        }catch (Exception e){
            log.error("UserController.login error:{}",e);
            return ResponseEntity.setForServiceError();
        }

    }

}
